#include "teachdelegate.h"
#include "MessageBox.h"
#include "models/operatelogmodel.h"
#include "qpainter.h"
#include "teachdialog.h"

#include <models/teachmodel.h>

#include <QEvent>
#include <QMouseEvent>
#include <mainwindow.h>

TeachDelegate::TeachDelegate(QObject *parent)
    : QStyledItemDelegate{parent}
{}

void TeachDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    // 保存画笔状态
    painter->save();
    // 文本区域需要右移，为复选框留出空间
    QRect textRect = option.rect;
    textRect.setLeft(20*g_xScale); // 复选框右侧留出10像素间距
    textRect.setRight(option.widget->rect().width() - 120*g_xScale); // 为两个按钮预留空间
    if(option.state&QStyle::State_Selected)
    {
        painter->fillRect(option.rect,QColor(72,73,78,255));
    }
    // 绘制文本
    QString text = index.data(TeachModel::NameRole).toString();
    QString elidedText = painter->fontMetrics().elidedText(text, Qt::ElideRight, textRect.width());
    painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, elidedText);

    // 绘制编辑和删除图标
    QIcon editIcon(":/images/edit.png");
    QIcon deleteIcon(":/images/delete.png");

    const int iconSize = 40*g_xScale;
    QRect editIconRect = option.rect;
    editIconRect.setLeft(option.widget->rect().width() - 100*g_xScale);
    editIconRect.setWidth(iconSize);
    editIconRect.setTop(option.rect.top() + (option.rect.height() - iconSize) / 2);
    editIconRect.setHeight(iconSize);

    QRect deleteIconRect = option.rect;
    deleteIconRect.setLeft(option.widget->rect().width() - 50*g_xScale);
    deleteIconRect.setWidth(iconSize);
    deleteIconRect.setTop(option.rect.top() + (option.rect.height() - iconSize) / 2);
    deleteIconRect.setHeight(iconSize);

    editIcon.paint(painter, editIconRect);
    deleteIcon.paint(painter, deleteIconRect);

    painter->restore();
}

bool TeachDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    if (event->type() == QEvent::MouseButtonRelease) {
        const QMouseEvent *mouseEvent = dynamic_cast<const QMouseEvent*>(event);
        if (!mouseEvent) return false;
        // 编辑图标区域
        const int iconSize = 40*g_xScale;
        QRect editIconRect = option.rect;
        editIconRect.setLeft(option.widget->rect().width() - 100*g_xScale);
        editIconRect.setWidth(iconSize);
        editIconRect.setTop(option.rect.top() + (option.rect.height() - iconSize) / 2);
        editIconRect.setHeight(iconSize);
        editIconRect = editIconRect.adjusted(-5*g_xScale, -5*g_xScale, 5*g_xScale, 5*g_xScale);

        // 删除图标区域
        QRect deleteIconRect = option.rect;
        deleteIconRect.setLeft(option.widget->rect().width() - 50*g_xScale);
        deleteIconRect.setWidth(iconSize);
        deleteIconRect.setTop(option.rect.top() + (option.rect.height() - iconSize) / 2);
        deleteIconRect.setHeight(iconSize);
        deleteIconRect = deleteIconRect.adjusted(-5*g_xScale, -5*g_xScale, 5*g_xScale, 5*g_xScale);

        TeachModel * pTeachModel = qobject_cast<TeachModel*>(model);
        if (!pTeachModel) return false;

        if (editIconRect.contains(mouseEvent->pos())) {
            // 处理编辑图标点击
            TeachDialog dlg(TeachDialog::EDIT_DIALOG,pTeachModel,index.row(),MainWindow::getInstance());
            dlg.setName(index.data(TeachModel::NameRole).toString());
            dlg.exec();
            return true;
        } else if (deleteIconRect.contains(mouseEvent->pos())) {
            // 处理删除图标点击
            if(question(MainWindow::getInstance(),tr("确定删除示教\"%1\"吗？").arg(index.data(TeachModel::NameRole).toString())) == 0)
            {
                pTeachModel->deleteTeach(index.row());
                OPERATE_LOG("删除示教："+index.data(TeachModel::NameRole).toString());
                //qDebug()<<index.row()<<"index.data(TeachModel::NameRole).toString()"<<index.data(TeachModel::NameRole).toString();
            }
            return true;
        }
    }
    return false;
}
